package experiment;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import vo.Status;
import Service.StatusService;
import Utils.Constants;
import Utils.DateUtils;
import Utils.FileUtils;
import Utils.MathUtils;
import Utils.SortUtils;
import Utils.Utils;

public class StabilityRanking {

	public static void computeUserIdStability() {
		// 读取时间参数
		Hashtable<String, Double> userId_stability = new Hashtable<String, Double>();
		Hashtable<String, String> params = FileUtils.readHashtableS(Constants.STATUS_TIME_PARAMS_PATH);
		String startTime = params.get("startTime");
		int days = Integer.parseInt(params.get("days"));

		ArrayList<String> userIds = Init.loadUserIds();
		int count = 0;
		for (String userId : userIds) {
			if (count % 100 == 0)
				System.out.println(count);
			count++;
			// 算一个用户每周微博频数统计
			ArrayList<Status> statuses = StatusService.getByUserId(userId);
			Hashtable<String, Integer> week_count = ActivityRanking.computeWeeklyStatuses(startTime, days, userId);
			double stability = 0.0;
			double contentStability = MathUtils.getVariance(Utils.values2ArrayList(week_count));// TODO:怎么没有除0错误呢

			ArrayList<Integer> keys = Utils.getKeysWithNonEmptyValues(week_count);
			keys = SortUtils.sort(keys, 1);
			double timeStability = Utils.computeTimeStability(keys);
			if (timeStability != 0)
				stability = statuses.size() / (timeStability * contentStability);// 值越大，越稳定
			else
				stability = 0;
			userId_stability.put(userId, MathUtils.precision(stability, Constants.PRECISION));
		}
		FileUtils.writeFileHashtable(userId_stability, Constants.USERID_STABILITY_PATH, Constants.DESC);
	}

	public static void main(String args[]) {
		computeUserIdStability();
	}

}
